﻿using System;
using System.Data.SqlClient;
using Devv.Core.UniDataTier;
using Yglu.BusinessLayer.Entities;

namespace Yglu.BusinessLayer
{
	public partial class DataAccess
	{
		#region Get

		public static SqlDataReader Terminal_Get()
		{
			DataHandler db = new DataHandler();

			return (SqlDataReader)db.ExecuteReader("upTerminal_Get");
		}

		public static SqlDataReader Terminal_Get(Int32 id)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@TerminalId", id);

			return (SqlDataReader)db.ExecuteReader("upTerminal_Get");
		}

		public static SqlDataReader Terminal_Get(String uniqueId)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@UniqueId", uniqueId);

			return (SqlDataReader)db.ExecuteReader("upTerminal_Get");
		}

		public static SqlDataReader Terminal_Get(Store store)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@StoreId", store.Id);

			return (SqlDataReader)db.ExecuteReader("upTerminal_Get");
		}

		#endregion

		#region Insert and update

		public static void Terminal_Upsert(Terminal terminal)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@StoreId", terminal.Store.Id);
				db.AddParameter("@UniqueId", terminal.UniqueId);
				db.AddParameter("@Active", terminal.Active);

				if (terminal.Id > 0)
				{
					db.AddParameter("@TerminalId", terminal.Id);
					db.ExecuteScalar("upTerminal_Update");
				}
				else
				{
					terminal.Id = Convert.ToInt32(db.ExecuteScalar("upTerminal_Insert"));
				}
			}
		}

		#endregion
	}
}